package com.hbase.reduce;

import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordCountReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, ImmutableBytesWritable, Mutation>.Context context) throws IOException, InterruptedException {
        int count = 0;
        // 遍历集合对每个单词出现次数累加
        for (IntWritable value : values) {
            count += value.get();
        }

        //创建插入数据对象put
        Put put = new Put(("rowKey_"+key).getBytes());
        // 添加列族、列、列值
        put.addColumn("info".getBytes(), key.toString().getBytes(), String.valueOf(count).getBytes());
        //写出
        context.write(null, put);
    }
}